<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Account extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
		$this->load->library('form_validation');
        $this->load->model('tank_auth/users','use');
        
    }

    /**
     * Default function
     */
    public function index($act=false)
    {
        if(!$this->tank_auth->is_logged_in())
            redirect('account/register');
        $this->load->model('orders');
        $profile = $this->use->get_profile($this->tank_auth->get_user_id());
        $this->assign('profile',$profile);
        $this->assign('orders',$this->orders->getOrder(false,$this->tank_auth->get_user_id()));
         //$post =$this->input->post(null);                
    }
    public function register(){
        
        
    }
    public function do_register(){
        $this->disable_view();
        $post =$this->input->post(null);
        global $site_info;
      	$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
		$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|email');
        $this->form_validation->set_rules('name', 'Fullname', 'required');
        
        if ($this->form_validation->run() == FALSE)
		{
			_set_error_message(validation_errors());
            redirect('account/register');
		}
		else
		{
		  if(isset($post['newjobs'])) $isnewsletter = 1;
          else $isnewsletter = 0;
			$rs = $this->tank_auth->create_user($post['name'],$post['email'],$post['password'],false,$isnewsletter);
            
                        if(!empty($rs))
                        {
                            $this->load->model('tank_auth/roles');
                            $this->roles->set_roles($rs['user_id'],$site_info['user_role']);
                            $profile = array('user_id'=>$rs['user_id']
                            ,'country'=>$post['slcity']
                           
                            ,'name'=>$post['name']
                        ,'birthdate'=>strtotime($post['slday'].'/'.$post['slmonth'].'/'.$post['slyear'])
                        ,'gender'=>$post['gender']
                        
                        );
                            $rs = $this->use->create_profile($profile);
                            _set_success_message('Save successful');
                        }
                        else{
                             _set_error_message('....');
                             redirect('account/register');
                        }
                           
                        redirect('account/login');
                        
		}
    }
    
    public function login(){ 
        if($this->tank_auth->is_logged_in())
            redirect('');
    }
  public function do_login()
  {
    $this->disable_view();
    
    $this->load->library('form_validation');
        $post = $this->input->post(null);
        if($this->tank_auth->is_logged_in())
            redirect('');
        $this->form_validation->set_rules('email', 'Email', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
        if ($this->form_validation->run() == FALSE)
		{
			_set_error_message(validation_errors());
            redirect('account/login');
		}
		else
		{
            
                if($this->tank_auth->login($post['email'],$post['password'],false,false,true)){
                    
                    redirect();
                }
                else{
                    _set_error_message('Your username and password is incorrect. Please try again...');
                    
                    redirect('account/login');
                }
        }
    }
    
    public function forget_password(){
        
    }
    function reset_password($user_id,$key){
        
    }
    public function send_password()
    {
        
        $post = $this->input->post(null);
      
        if(!$post['email']){
            _set_error_message('Please fill email address');
            redirect('account/forget_password');
        }
       
        if($this->tank_auth->is_email_available($post['email']))
        {
            $data = $this->tank_auth->forgot_password($post['email']);
            
            if($this->_send_mail('forgot-password',$post['email'],$data)){
                $data = 'Ðã gửi email reset password';
            }else{
                $data = 'Không thể gửi mail';
            }
        }else{
            $data ='Không tìm thấy trong CSDL';
        }
        $this->assign('data',$data);
        redirect();    
        
    }
    
    public function can_reset_password(){
        $this->disable_view();
        $post = $this->input->post(null);
        $this->form_validation->set_rules('password', 'New Password', 'required|matches[passconf]');
		$this->form_validation->set_rules('passconf', 'Confirm new password', 'required');
        if ($this->form_validation->run() == FALSE)
		{
		  
		}else{
		  if($this->tank_auth->can_reset_password($post['user_id'],$post['key'])){
		      $this->tank_auth->reset_password($post['user_id'],$post['key'],$post['password']);
              $data['password'] = $post['password'];
            
              $this->_send_mail('forgot-password','harry88pham@gmail.com',$data);
             redirect('');
		  }
		}
    }
    
    public function _send_mail($type,$email,$data){
        $this->disable_view();
        $config['protocol']='smtp';  
        $config['smtp_host']='ssl://smtp.gmail.com';  
        $config['smtp_port']='465';  
        $config['smtp_timeout']='3000';  
        $config['smtp_user']='harry88pham@gmail.com';  
        $config['smtp_pass']='05111988';  
        $config['charset']='utf-8';  
        $config['newline']="\r\n"; 
        $config['mailtype'] = 'html'; 
        $config['wordwrap'] = FALSE;
        
        $this->load->library('email',$config);
        $this->email->from('harry88pham@gmail.com');
              $this->email->to($email);

              $this->email->subject('tesstng');
              $this->email->message($this->load->view('layouts/email/'.$type.'-html',$data,true));  
        $this->email->send();
        
      
    }
    public function check_email(){
        $this->disable_view();
        echo $this->tank_auth->is_email_available($this->input->post('email'));
    }
    public function logout(){
        $this->disable_view();
        $this->tank_auth->logout();
        redirect(''); 
    }
    
    
    
}